package com.changgou.good.controller;

import com.changgou.good.service.SpuService;
import com.changgou.goods.pojo.Goods;
import com.changgou.goods.pojo.Spu;
import com.github.pagehelper.PageInfo;
import com.mysql.cj.x.protobuf.Mysqlx;
import entity.Result;
import entity.StatusCode;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/spu")
@CrossOrigin
public class SpuController {

    @Autowired
    private SpuService spuService;

    @PostMapping("/save")
    public Result save(@RequestBody Goods goods){
        SpuService.saveGoods(goods);
        return new Result(true, StatusCode.OK,"保存成功");
    }

    @GetMapping("/{id}")
    public Result<Goods> findGoodsById(@PathVariable Long id){
        Goods goods = spuService.findByGoodsById(id);
        return new Result<Goods>(true, StatusCode.OK,"查询成功",goods);
    }

    @GetMapping("/update")
    public Result update(@RequestBody Goods goods){
        SpuService.saveGoods(goods);
        return new Result(true,StatusCode.OK,"修改成功",goods);
    }

    @PutMapping("/audit/{id}")
    public Result audit(@PathVariable Long id){
        spuService.audit(id);
        return new Result(true,StatusCode.OK,"审核成功");
    }

    @DeleteMapping("/logic/delete/{id}")
    public Result logicDelete(@PathVariable Long id){
        spuService.logicDelete(id);
        return new Result(true,StatusCode.OK,"逻辑删除成功");
    }

    @PutMapping("/restore/{id}")
    public Result restore(@PathVariable Long id){
        spuService.restore(id);
        return new Result(true,StatusCode.OK,"数据恢复成功");
    }

    @PostMapping(value = "/search/{page}/{size}")
    public Result<PageInfo> findPage(@RequestBody(required = false)Spu spu,
                                     @PathVariable int page,
                                     @PathVariable int size){
        PageInfo<Spu> pageInfo = spuService.findPage(spu,page,size);
        return new Result(true,StatusCode.OK,"查询成功",pageInfo);
    }

}
